<template>
  <div class="content">
    <div class="title">
      <img src="@/assets/logo_82x82.png" />
      <div>监考系统</div>
    </div>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      class="demo-ruleForm"
    >
      <el-form-item prop="pass">
        <el-input
          v-model="ruleForm.pass"
          type="type"
          autocomplete="off"
          :status-icon="true"
          placeholder="请输入您的账号"
        />
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          v-model="ruleForm.checkPass"
          type="password"
          show-password
          :status-icon="true"
          autocomplete="off"
          placeholder="请输入您的密码"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang='ts' setup>
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance } from "element-plus";
import { Hide, View } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
const router = useRouter();
const { proxy }: any = getCurrentInstance();
const ruleFormRef = ref<FormInstance>();
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入管理员登录名"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入登录密码"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  pass: "",
  checkPass: "",
});

const rules = reactive({
  pass: [{ validator: validatePass, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: any) => {
    if (valid == true) {
      proxy.$http
        .post("/invigilate/examinee/user/login", {
          userName: ruleForm.pass,
          password: ruleForm.checkPass,
          deviceType: "web",
        })
        .then((res: any) => {
          //   console.log("登录接口获取的数据", res);

          console.log("登录成功", res);
          if (res.data.code == 0) {
            window.sessionStorage.setItem("token", res.data.accessToken);
            window.sessionStorage.setItem("uid", res.data.uid);
            ElMessage({
              message: res.data.errorMsg,
              type: "success",
            });
            router.push({
              path: "/menuManagement",
              query: {},
            });
          } else {
            ElMessage.error(res.data.errorMsg);
          }
        });
    } else {
      return false;
    }
  });
};
</script>

<style lang='scss' scoped>
@import "@/style/administratorLogin/administratorLogin.scss";
</style>
<style lang="less">
.el-input {
  width: 360px;
  height: 46px;
  margin-top: 10px;
}
.el-form-item__error {
  margin-top: 8px;
  font-family: SourceHanSansCN-Normal;
  font-size: 12px;
  color: #1890ff;
}
</style>